package d;
import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-05-29
 * Time: 17:54
 */
public class MyLinkedList {
    //节点结构
    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode createList(String str){
        LinkedList<ListNode> list = new LinkedList<>();
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            int n = Integer.valueOf(ch)-48;
            ListNode node = new ListNode(n);
            list.add(node);
        }
        for (int i = 0; i < list.size()-1; i++) {
            ListNode node  = list.get(i);
           ListNode node2 =list.get(i+1);
           node.next = node2;
        }
        ListNode last = list.get(list.size()-1);
        last.next = null;
        return list.get(0);
    }
    //判断集合 A 和 B 是否相等
    public boolean  isEqual(ListNode curA , ListNode curB){
        if(curA == null && curB == null){
            return true;
        }
        if(curA != null && curB == null || curA == null && curB !=null){
            return false;
        }
        //curA != null && curB != null
        ListNode pa = curA;
        ListNode pb =curB;
        while(pa != null && pb != null){
           if(pa.val != pb.val){
               return false;
           }
           pa =pa.next;
           pb = pb.next;
        }
        if(pa == null && pb == null){
            return true;
        }else{
            return false;
        }
    }
    //求集合 A 和 B 的交集
    /**
     *
     * @param curA: 表示链表A的头结点
     * @param curB: 表示链表B的头结点
     * @return
     */
    public ListNode Interest(ListNode curA , ListNode curB){
        if(curA == null && curB == null){
            return null;
        }
        if(curA != null && curB == null || curA == null && curB !=null){
            return null;
        }
        //curA != null && curB != null
        ListNode pa = curA;
        ListNode pb =curB;
        ListNode prev =null;
        while(pa != null && pb !=null){
            if(pa.val < pb.val){
                if(pa == curA){
                    curA = curA.next;
                    pa = pa.next;
                }else {
                    prev.next = pa.next;
                    pa = pa.next;
                }
            }else if(pa.val > pb.val){
                pb = pb.next;
            }else {
                prev = pa;
                pa = pa.next ;
                pb =pb.next;
            }
        }
//        prev.next = null;
        return curA ;
    }
    //求集合 A 和 B 的并集
    public ListNode union(ListNode curA , ListNode curB){
        ListNode pb = curB;
        int flg = 0 ;
        ListNode prev = null;
        while(pb != null){
            ListNode pbNext = pb.next;
            flg = 0;
            ListNode pa = curA ;
            prev = null;
            while(pa != null){
                if(pa.val == pb.val){
                    flg = 1;
                    break;
                }
                prev = pa;
                pa = pa.next;
            }
            if(flg == 0){
                prev .next = pb;
                prev = prev.next;
                prev.next = null;
            }
            pb =pbNext;

        }

        return curA;
    }
    //差集
    public ListNode subtraction(ListNode curA , ListNode curB) {
        ListNode pb =curB;
         while(pb != null) {
            ListNode pa =curA;
            ListNode prev = null;
            while(pa != null){
                if(pa.val == pb.val){
                    if(pa == curA){
                        curA = curA.next;
                    }else {
                        prev.next = pa.next;
                    }
                    break;
                }
                //不相等,改变前驱节点
                prev = pa;
                pa = pa.next;
            }
            pb =pb.next;
        }
       return curA;
    }
    public void print(ListNode cur){
        while(cur != null){
            System.out.print(cur.val + " ");
            cur =cur.next;
        }
        System.out.println();
    }

}
